﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using System.Collections;
using System.Web.Routing;
using FreedomERP.Services.Inv;
using FreedomERP.Services.Bd;
using FreedomERP.Services.Inv.Impl;
using FreedomERP.Services.Bd.Impl;
using FreedomERP.Comm;
using FreedomERP.Models;
using FreedomERP.Models.Inv;
using FreedomERP.Models.Bd;
using FreedomERP.Filter;

namespace FreedomERP.Controllers
{
    [CheckSessionFilterAttribute]
    public class ShortageOrderController : Controller
    {

        IInvShortageOrderService shortageOrderService { get; set; }
        IBdCategoryService categoryService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (shortageOrderService == null)
            {
                shortageOrderService = new InvShortageOrderServiceImpl();
            }
            if (categoryService == null)
            {
                categoryService = new BdCategoryServiceImpl();
            }

            base.Initialize(requestContext);
        }

        public ActionResult ShortageOrderIndex()
        {
            return View();
        }

        /// <summary>
        /// 缺货单列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ShortageOrderListPaging(int start, int limit, String FCustomerId, String FSendOrderNum, String FBillDateFrom, String FBillDateTo, String FStatusId, String FProductId)
        {
            int totalCount = 0;

            Hashtable filters = new Hashtable();
            filters.Add("FCustomerIdParam", FCustomerId);
            filters.Add("FSendOrderNumParam", FSendOrderNum);
            filters.Add("FBillDateFrom", FBillDateFrom);
            filters.Add("FBillDateTo", FBillDateTo);

            String statusId = null;
            if (FStatusId != null || !ConstData.EMPTY_STRING.Equals(FStatusId))
                statusId = FStatusId;
            filters.Add("FStatusIdParam", statusId);
            filters.Add("FProductIdParam", FProductId);

            PagingParams pgp = new PagingParams(null, OrderDirection.ToDirection("desc"), start, limit, filters);

            List<InvShortageOrderView> listTemp = shortageOrderService.QueryInvShortageOrderPaggingByParam(pgp, out totalCount);
            AjaxStoreResult orders = new AjaxStoreResult(listTemp, totalCount);
            return orders;
        }


        /// <summary>
        /// 缺货单列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ShortageOrderStatusList()
        {
            int totalCount = 0;
            List<BdCategory> listTemp = categoryService.findInvShortageOrderStatusCategories(null, 0, int.MaxValue, out totalCount);
            List<BdCategoryView> resultList = new List<BdCategoryView>();
            foreach (var cate in listTemp)
            {
                BdCategoryView cateView = new BdCategoryView();
                cateView.FID = cate.FId;
                if (cate.FName == "Valid")
                {
                    cateView.FName = "欠货";
                }
                else if (cate.FName == "Invalid")
                {
                    cateView.FName = "已核销";
                }
                else
                {
                    cateView.FName = "未知";
                }
                resultList.Add(cateView);
            }
            AjaxStoreResult orderStatus = new AjaxStoreResult(resultList, totalCount);
            return orderStatus;
        }


        public ActionResult CloseShortageOrder(String fId)
        {
            AjaxResult response = new AjaxResult();
            try
            {
                shortageOrderService.closeShortageOrder(fId, BaseController.GetCurrLoginAccountFId());
            }
            catch (System.Exception ex)
            {
                response.Result = false;
                response.ErrorMessage = ex.Message;
                return response;
            }
            response.Result = true;
            return response;
        }


        /// <summary>
        /// 缺货单详情列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ShortageOrderDetailListPaging(int start, int limit, String Fid)
        {
            int totalCount = 0;

            Hashtable filters = new Hashtable();
            filters.Add("Fid", Fid);
            PagingParams pgp = new PagingParams(null, OrderDirection.ToDirection("desc"), start, limit, filters);

            List<InvShortageOrderDetailView> listTemp = shortageOrderService.QueryInvShortageOrderDetailPaggingByParam(pgp, out totalCount);
            AjaxStoreResult orders = new AjaxStoreResult(listTemp, totalCount);
            return orders;
        }


        /// <summary>
        /// 缺货单详情列表
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateShortageOrderDetail(String fId, String fParentId, String returnNum)
        {

            String resultFID = null;
            AjaxResult response = new AjaxResult();
            try
            {
                resultFID = shortageOrderService.UpdateShortageOrderDetail(fId, fParentId, returnNum, BaseController.GetCurrLoginAccountFId());
            }
            catch (System.Exception ex)
            {
                response.Result = false;
                response.ErrorMessage = ex.Message;
                return response;
            }
            Parameter pFId = new Parameter();
            pFId.Name = "FId";
            pFId.Value = resultFID;
            response.ExtraParamsResponse.Add(pFId);
            return response;
        }

        /// <summary>
        /// 导出缺货单信息到excel
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public ActionResult ExportShortageOrderToExcel(String FId)
        {
            string serverPath = Server.HtmlEncode(Request.PhysicalApplicationPath);
            shortageOrderService.ExportShortageOrderToExcel(FId, serverPath, Response);
            return null;
        }
    }
}
